home *** CD-ROM | disk | FTP | other *** search
/ PC World Komputer 2010 April / PCWorld0410.iso / pluginy Firefox / 3794 / 3794.xpi / chrome / content / toolbar.js < prev    next >
Text File  |  2009-11-16  |  16KB  |  363 lines

  1. /**
  2.  *
  3.  * The source code included in this file is licensed to you by Facebook under
  4.  * the Apache License, Version 2.0.  Accordingly, the following notice
  5.  * applies to the source code included in this file:
  6.  *
  7.  * Copyright ┬⌐ 2009 Facebook, Inc.
  8.  *
  9.  * Licensed under the Apache License, Version 2.0 (the "License"); you may
  10.  * not use this file except in compliance with the License. You may obtain
  11.  * a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
  12.  *
  13.  * Unless required by applicable law or agreed to in writing, software
  14.  * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  15.  * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
  16.  * License for the specific language governing permissions and limitations
  17.  * under the License.
  18.  *
  19.  */
  20.  
  21.  
  22. var Cc = Components.classes;
  23. var Ci = Components.interfaces;
  24.  
  25. var fbSvc = Cc['@facebook.com/facebook-service;1'].getService(Ci.fbIFacebookService);
  26. var obsSvc = Cc["@mozilla.org/observer-service;1"].getService(Ci.nsIObserverService);
  27.  
  28. debug( "toolbar.js" );
  29.  
  30. var topicToXulId =  { 'facebook-msgs-updated':      'facebook-notification-msgs'
  31.                     , 'facebook-pokes-updated':     'facebook-notification-poke'
  32.                     , 'facebook-reqs-updated':      'facebook-notification-reqs'
  33.                     , 'facebook-event-invs-updated':'facebook-notification-event-invs'
  34.                     , 'facebook-group-invs-updated':'facebook-notification-group-invs'
  35.                     };
  36.  
  37. function checkSeparator(data) {
  38.     var showSep = false;
  39.     for each( var elt_id in topicToXulId ) {
  40.         if( getAttributeById( elt_id, 'label') != "0" ) {
  41.             showSep = true;
  42.             break;
  43.         }
  44.     }
  45.     debug( 'showSep', showSep );
  46.     setAttributeById( 'facebook-notification-separator', 'hidden'
  47.                     , showSep ? 'false': 'true' );
  48. }
  49.  
  50. var fbToolbarObserver = {
  51.     observe: function(subject, topic, data) {
  52.         debug('toolbar observing something: ', topic);
  53.         var fStrings = GetFBStringBundle();
  54.         var eltId = topicToXulId[topic];
  55.         if( eltId ) {
  56.             setAttributeById(eltId, 'label', data);
  57.             checkSeparator(data);
  58.         }
  59.         else {
  60.             var statusBox;
  61.             switch (topic) {
  62.             case 'facebook-session-start':
  63.                 subject = subject.QueryInterface(Ci.fbIFacebookUser);
  64.                 setAttributeById('facebook-name-info', 'label', subject.name);
  65.                 statusBox = document.getElementById('facebook-toolbar-status');
  66.                 statusBox.style.display="block";
  67.                 statusBox.value = subject.status;
  68.                 facebook.onStatusBoxBlur(statusBox); // change color for emptyText
  69.                 setAttributeById('facebook-name-info', 'userid', subject.id);
  70.                 setAttributeById('facebook-menu-my-profile', 'userid', subject.id);
  71.                 setAttributeById('facebook-login-status', 'label', fStrings.getString('logout'));
  72.                 setAttributeById('facebook-login-status', 'tooltiptext', fStrings.getString('logout'));
  73.                 var sb = GetFBSearchBox();
  74.                 if (sb.value != fStrings.getString('searchplaceholder') && sb.value != '') {
  75.                     sb.value = '';
  76.                     facebook.searchBoxBlur(sb);
  77.                 }
  78.                 SetHint(true, fStrings.getString('loadingfriends'), '');
  79.                 break;
  80.             case 'facebook-session-end':
  81.                 debug('ending session...');
  82.                 setAttributeById('facebook-login-status', 'label', fStrings.getString('login'));
  83.                 setAttributeById('facebook-login-status', 'tooltiptext', fStrings.getString('login'));
  84.                 setAttributeById('facebook-name-info', 'label', '');
  85.                 statusBox = document.getElementById('facebook-toolbar-status');
  86.                 statusBox.style.display="none";
  87.                 for each( var top in topicToXulId )
  88.                     setAttributeById( top, 'label', '?');
  89.                 facebook.clearFriends(true);
  90.                 break;
  91.             case 'facebook-friends-updated':
  92.                 facebook.loadFriends();
  93.                 break;
  94.             case 'facebook-new-friend':
  95.             case 'facebook-friend-updated':
  96.                 debug( 'friend update...' );
  97.                 subject = subject.QueryInterface(Ci.fbIFacebookUser);
  98.                 facebook.updateFriend(subject);
  99.                 break;
  100.             case 'facebook-status-updated':
  101.                 statusBox = document.getElementById('facebook-toolbar-status');
  102.                 statusBox.value = data;
  103.                 facebook.onStatusBoxBlur(statusBox);
  104.                 break;
  105.             case 'facebook-new-day':
  106.                 facebook.clearFriends(false);
  107.                 facebook.loadFriends();
  108.                 break;
  109.             }
  110.         }
  111.     }
  112. };
  113.  
  114. var progListener = {
  115.     onLocationChange: function(webProgress, request, location) {
  116.         if (fbSvc.loggedIn) {
  117.             fbSvc.hintPageLoad(IsFacebookLocation(location));
  118.         }
  119.     },
  120.     onProgressChange: function(webProgress, request, curSelfProg, maxSelfProg, curTotalProg, maxTotalProg) {  },
  121.     onSecurityChange: function(webProgress, request, state) {  },
  122.     onStateChange: function(webProgress, request, stateFlags, status) {  },
  123.     onStatusChange: function(webProgress, request, status, message) {  }
  124. };
  125.  
  126. var topics_of_interest =    [ 'facebook-session-start'
  127.                             , 'facebook-friends-updated'
  128.                             , 'facebook-friend-updated'
  129.                             , 'facebook-new-friend'
  130.                             , 'facebook-session-end'
  131.                             , 'facebook-msgs-updated'
  132.                             , 'facebook-pokes-updated'
  133.                             , 'facebook-event-invs-updated'
  134.                             , 'facebook-group-invs-updated'
  135.                             , 'facebook-reqs-updated'
  136.                             , 'facebook-new-day'
  137. //                            , 'facebook-status-set-result'
  138.                             , 'facebook-status-updated'
  139.                             ];
  140.  
  141. var facebook = {
  142.     load: function() {
  143.         debug( "loading toolbar..." );
  144.         facebook.fStringBundle = GetFBStringBundle();
  145.         debug(facebook.fStringBundle.src);
  146.         var prefSvc = Cc['@mozilla.org/preferences-service;1'].getService(Ci.nsIPrefBranch);
  147.         if (!prefSvc.prefHasUserValue('extensions.facebook.not_first_run')) {
  148.           // unfortunately if we create any tabs here, session store overrides
  149.           // them, so instead we'll create a tab in 250 ms, hopefully after
  150.           // session store does its business.
  151.           window.setTimeout("getBrowser().loadOneTab('chrome://facebook/content/welcome.html', null, null, null, false, false)", 250);
  152.           prefSvc.setBoolPref('extensions.facebook.not_first_run', true);
  153.           prefSvc.lockPref('extensions.facebook.not_first_run');
  154.         }
  155.         document.getElementById('facebook-search').addEventListener('keypress', HandleKeyPress, true);
  156.         for each ( var topic in topics_of_interest ) {
  157.             debug( "observer added", topic );
  158.             obsSvc.addObserver(fbToolbarObserver, topic, false);
  159.         }
  160.  
  161.         var loggedInUser = fbSvc.loggedInUser;
  162.         if (loggedInUser) {
  163.             loggedInUser = loggedInUser.QueryInterface(Ci.fbIFacebookUser);
  164.             setAttributeById('facebook-name-info', 'label', loggedInUser.name);
  165.             setAttributeById('facebook-name-info', 'userid', loggedInUser.id);
  166.             setAttributeById('facebook-toolbar-status', 'value', loggedInUser.status);
  167.             setAttributeById('facebook-login-status', 'label', facebook.fStringBundle.getString('logout'));
  168.             setAttributeById('facebook-login-status', 'tooltiptext', facebook.fStringBundle.getString('logout'));
  169.             setAttributeById('facebook-menu-my-profile', 'userid', loggedInUser.id);
  170.             setAttributeById('facebook-notification-msgs', 'label', fbSvc.numMsgs);
  171.             setAttributeById('facebook-notification-poke', 'label', fbSvc.numPokes);
  172.             setAttributeById('facebook-notification-reqs', 'label', fbSvc.numReqs);
  173.             setAttributeById('facebook-notification-group-invs', 'label', fbSvc.numGroupInvs);
  174.             setAttributeById('facebook-notification-event-invs', 'label', fbSvc.numEventInvs);
  175.  
  176.             var statusBox = document.getElementById('facebook-toolbar-status');
  177.             statusBox.style.display="block";
  178.             facebook.onStatusBoxBlur(statusBox); // change color for emptyText
  179.         } else {
  180.           fbSvc.savedSessionStart();
  181.         }
  182.         facebook.loadFriends();
  183.         getBrowser().addProgressListener(progListener);
  184.         debug('facebook toolbar loaded.');
  185.         },
  186.     unload: function() {
  187.         for each (var topic in topics_of_interest)
  188.             obsSvc.removeObserver(fbToolbarObserver, topic);
  189.         if( fbSvc.loggedInUser )
  190.  
  191.         debug('facebook toolbar unloaded.');
  192.     },
  193.     sortFriends: function(f1, f2) {
  194.         var n1 = f1.name.toLowerCase();
  195.         var n2 = f2.name.toLowerCase();
  196.         if (n1 < n2) return -1;
  197.         else if (n1 > n2) return 1;
  198.         else return 0;
  199.     },
  200.   loadFriends: function() {
  201.     debug('loadFriends()');
  202.     var list = document.getElementById('PopupFacebookFriendsList');
  203.     if (list.firstChild && list.firstChild.id != 'FacebookHint') {
  204.       return;
  205.     }
  206.     list.selectedIndex = -1;
  207.     var count = {};
  208.     var friends = fbSvc.getFriends(count);
  209.     debug('got friends', count.value);
  210.     if (!fbSvc.loggedIn) {
  211.       var lfLoad = facebook.fStringBundle.getString('loadFriends');
  212.       SetHint(true, lfLoad, 'FacebookLogin()');
  213.     } else if (!count.value) {
  214.       SetHint(true, facebook.fStringBundle.getString('loadingfriends'), '');
  215.     } else {
  216.       friends.sort(this.sortFriends);
  217.       for each (var friend in friends) {
  218.         this.createFriendNode(list, friend, null);
  219.       }
  220.       if (!IsSidebarOpen()) {
  221.         SearchFriends(GetFBSearchBox().value);
  222.       }
  223.     }
  224.   },
  225.   updateFriend: function(friend) {
  226.     debug( 'updating friend...' );
  227.     var elem = document.getElementById('popup-' + friend.id);
  228.     var list = document.getElementById('PopupFacebookFriendsList');
  229.     this.createFriendNode(list, friend, elem);
  230.   },
  231.   createFriendNode: function(list, friend, elem) { // creates nodes in the search popup only
  232.     if (!friend.name) return;
  233.     if (!elem) {
  234.       var item = document.createElement('richlistitem');
  235.       item.setAttribute('id', 'popup-' + friend.id);
  236.       item.setAttribute('class', 'popupFriendBox');
  237.     } else {
  238.       var item = elem;
  239.     }
  240.     item.setAttribute('friendname', friend.name);
  241.     var firstName = friend.name.substr(0, friend.name.indexOf(' '));
  242.     if (!firstName) firstName = friend.name;
  243.     item.setAttribute('firstname', firstName);
  244.     SetStatus(item, friend.status, friend.stime);
  245.     item.setAttribute('ptime', getProfileTime(friend.ptime) );
  246.  
  247.     item.setAttribute('onmouseover', "SelectItemInList(this, this.parentNode)");
  248.     item.setAttribute('onmousedown', "this.doCommand();");
  249.     item.setAttribute('oncommand', "OpenFBUrl('profile.php', '" + friend.id + "', event)");
  250.     item.setAttribute('onclick', "checkForMiddleClick(this, event)" );
  251.     item.setAttribute('userid', friend.id);
  252.     item.setAttribute('pic', friend.pic);
  253.     if (!elem) {
  254.       // Note that this will put new friends at the bottom instead of alphabetized, but I think that's ok.
  255.       // It would get fixed in any new windows or when the browser restarts.
  256.       list.insertBefore(item, document.getElementById('FacebookHint'));
  257.     }
  258.   },
  259.   searchBoxFocus: function(searchBox) {
  260.     if (searchBox.value == facebook.fStringBundle.getString('searchplaceholder')) {
  261.       searchBox.value='';
  262.       searchBox.style.color='#000000';
  263.     }
  264.     if (!this.ignoreBlur && !IsSidebarOpen()) {
  265.       var popupElt = document.getElementById('PopupFacebookFriends');
  266.       if (popupElt.openPopup) {
  267.         popupElt.openPopup(searchBox, 'after_start', 0, 0, false, true);
  268.       } else {
  269.         popupElt.showPopup(searchBox, -1, -1, 'popup', 'bottomleft', 'topleft');
  270.       }
  271.       // if the sidebar was just open then we would be out of sync, so let's just filter the list to be safe
  272.       if (fbSvc.loggedIn) {
  273.         SearchFriends(searchBox.value);
  274.       }
  275.     }
  276.   },
  277.   searchBoxBlur: function(searchBox) {
  278.     if (!this.ignoreBlur) {
  279.       document.getElementById('PopupFacebookFriends').hidePopup();
  280.     }
  281.     if (searchBox.value=='') {
  282.       searchBox.style.color='#808080';
  283.       searchBox.value = facebook.fStringBundle.getString('searchplaceholder');
  284.     }
  285.   },
  286.   isEmptyStatusText: function (text) {
  287.     return '' == text.trim();
  288.   },
  289.   onStatusBoxFocus: function(statusBox) {
  290.     if (this.isEmptyStatusText(statusBox.value)) {
  291.       statusBox.value = '';
  292.     }
  293.     statusBox.style.color = '#000000';
  294.     statusBox.select();
  295.   },
  296.   onStatusBoxBlur: function(statusBox) {
  297.     if (this.isEmptyStatusText(statusBox.value)) {
  298.       statusBox.value = ''; // rely on the emptyText attribute
  299.       statusBox.style.color = '#808080';
  300.     } else {
  301.       statusBox.style.color = '#000000';
  302.     }
  303.   },
  304.   share: function() {
  305.     // not only do we need to encodeURIComponent on the string, we also need to escape quotes since
  306.     // we are putting this into a string to evaluate (as opposed to evaluating it directly)
  307.     var enc = function(str) {
  308.       return encodeURIComponent(str).replace("'", "\\'", 'g');
  309.     };
  310.     var p = '.php?src=tb&v=4&u=' + enc(content.document.location.href) + '&t=' + enc(document.title);
  311.     var openCmd = "window.open('http://www.facebook.com/sharer" + p
  312.       + "', 'sharer','toolbar=no,status=yes,resizable=yes,width=626,height=436');";
  313.     try {
  314.       // If we're not on a facebook page, just jump down to the catch block and open the popup...
  315.       if (!IsFacebookLocation(content.document.location))
  316.         throw null;
  317.       // We're on a facebook page, so let's try using share_internal_bookmarklet...
  318.  
  319.       // We can access the function easily through content's wrappedJSObject, but unfortunately if
  320.       // we try calling it directly, then the relative URL's in XMLHttpRequests are interpretted
  321.       // relative to our current chrome:// url and fail.  So instead we check for the function...
  322.       if (!content.wrappedJSObject.share_internal_bookmarklet)
  323.           throw null;
  324.       // ...and if the function is there then we have to do this lame javascript: url hack to
  325.       // execute it.
  326.       content.document.location = 'javascript:try { share_internal_bookmarklet("' + p +
  327.         '"); } catch (e) { setTimeout("' + openCmd + '", 0); } void(0);';
  328.     } catch(e) {
  329.       debug('title is: ' + document.title, 'url: ' + content.document.location.href, openCmd);
  330.       eval(openCmd);
  331.     }
  332.   },
  333.   photoupload: function() {
  334.     var wm = Components.classes["@mozilla.org/appshell/window-mediator;1"]
  335.                    .getService(Components.interfaces.nsIWindowMediator);
  336.     var win = wm.getMostRecentWindow("facebook:photoupload");
  337.     if (win) {
  338.         win.focus();
  339.     }
  340.     else
  341.     {
  342.         window.openDialog('chrome://facebook/content/photoupload/photoupload.xul',
  343.                           'facebook:photoupload', 'chrome,titlebar,toolbar,dialog=no,resizable');
  344.     }
  345.   },
  346.   clearFriends: function(sessionEnded) {
  347.     var list = document.getElementById('PopupFacebookFriendsList');
  348.     while (list.firstChild && list.firstChild.id != 'FacebookHint') {
  349.       list.removeChild(list.firstChild);
  350.     }
  351.     document.getElementById('PopupMessager').style.display = 'none';
  352.     document.getElementById('PopupPoker').style.display = 'none';
  353.     document.getElementById('PopupPoster').style.display = 'none';
  354.     if (sessionEnded) {
  355.       SetHint(true, facebook.fStringBundle.getString('loadFriends'), 'FacebookLogin()');
  356.     }
  357.   }
  358. };
  359. window.addEventListener('load', facebook.load, false);
  360. window.addEventListener('unload', facebook.unload, false);
  361.  
  362. debug('loaded toolbar.js');
  363.